package com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.entity.ErpFinancereceipt;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.entity.ErpFinancereceiptbill;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.mapper.ErpFinancereceiptbillMapper;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.dto.ErpFinancereceiptListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.mapper.ErpFinancereceiptMapper;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.service.IErpFinancereceiptService;
import com.xjrsoft.core.mp.base.BaseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-09-23
 */
@Service
@AllArgsConstructor
public class ErpFinancereceiptServiceImpl extends BaseService<ErpFinancereceiptMapper, ErpFinancereceipt> implements IErpFinancereceiptService {

	private ErpFinancereceiptbillMapper erpFinancereceiptbillMapper;

	@Override
	public IPage<ErpFinancereceipt> getPageList(ErpFinancereceiptListDto pageListDto) {
		Wrapper<ErpFinancereceipt> wrapper = Wrappers.<ErpFinancereceipt>query().lambda()				.eq(!StringUtil.isEmpty(pageListDto.getF_ReceiptCode()), ErpFinancereceipt::getFReceiptcode, pageListDto.getF_ReceiptCode())
				.like(!StringUtil.isEmpty(pageListDto.getF_Manager()), ErpFinancereceipt::getFManager, pageListDto.getF_Manager())
				.like(!StringUtil.isEmpty(pageListDto.getF_ClientId()), ErpFinancereceipt::getFClientid, pageListDto.getF_ClientId());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addErpFinancereceipt(ErpFinancereceipt erpFinancereceipt, List<ErpFinancereceiptbill> erpFinancereceiptbillList) {
		String erpFinancereceiptId = IdWorker.get32UUID();
		erpFinancereceipt.setFId(erpFinancereceiptId);
		erpFinancereceiptbillList.forEach(erpFinancereceiptbill -> erpFinancereceiptbill.setFReceiptid(erpFinancereceiptId));
		this.saveBatch(erpFinancereceiptbillList, ErpFinancereceiptbill.class, ErpFinancereceiptbillMapper.class);
		boolean isSuccess = this.save(erpFinancereceipt);
		return isSuccess ? erpFinancereceiptId : null;
	}

	@Override
	public boolean updateErpFinancereceipt(String id, ErpFinancereceipt erpFinancereceipt, List<ErpFinancereceiptbill> erpFinancereceiptbillList) {
		erpFinancereceipt.setFId(id);
		erpFinancereceiptbillList.forEach(erpFinancereceiptbill -> erpFinancereceiptbill.setFReceiptid(id));
		erpFinancereceiptbillMapper.delete(Wrappers.<ErpFinancereceiptbill>query().lambda().eq(ErpFinancereceiptbill::getFReceiptid, id));
		this.saveBatch(erpFinancereceiptbillList, ErpFinancereceiptbill.class, ErpFinancereceiptbillMapper.class);
		return this.updateById(erpFinancereceipt);
	}

	public List<ErpFinancereceiptbill> getErpFinancereceiptbillByParentId(String parentId){
		Wrapper wrapper = Wrappers.<ErpFinancereceiptbill>query().lambda().eq(ErpFinancereceiptbill::getFReceiptid, parentId);
		return erpFinancereceiptbillMapper.selectList(wrapper);
	}
}
